package com.douqu.game.auth.service;

import com.alibaba.fastjson.JSONObject;
import com.douqu.game.auth.database.mapper.OrderRecordMapper;
import com.douqu.game.auth.database.mapper.RoleRecordMapper;
import com.douqu.game.auth.database.model.RoleRecordModel;
import com.douqu.game.auth.utils.TalkingDataUtils;
import com.douqu.game.core.database.model.OrderRecordModel;
import com.douqu.game.core.e.E_RechargeStatus;
import com.douqu.game.core.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: Gavin.
 * Description:
 * Date: 2018/5/28 0028 下午 6:29
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
@Service
public class OrderRecordServiceImpl implements OrderRecordService {

    @Autowired
    private OrderRecordMapper orderRecordMapper;
    @Autowired
    private RoleRecordMapper roleRecordMapper;

    @Override
    public int createOrder(OrderRecordModel orderRecordModel) {
        orderRecordModel.setCreateTime(new Date());
        if (orderRecordModel.getStatus() == E_RechargeStatus.PAY_SUCCESS.getCode()){
            //完成
            orderRecordModel.setCompleteTime(new Date());
        }
        int result = orderRecordMapper.insert(orderRecordModel);
        try {
            if (orderRecordModel.getPartner() == 0 || orderRecordModel.getPartner() == 0){
                RoleRecordModel recordModel = roleRecordMapper.getByObjectIndex(orderRecordModel.getObjectIndex());
                if (recordModel == null){
                    throw new Exception("玩家不存在请核对流水号 objectIndex->" + orderRecordModel.getObjectIndex());
                }
                if (recordModel.getServerRuleId() == null || recordModel.getServerRuleId() == 0){
                    throw new Exception("玩家为老玩家未保存serverRuleId 无法进行离线talkingData数据发送");
                }
                orderRecordModel.setPartner(recordModel.getServerRuleId());
            }
            TalkingDataUtils.bq.add(TalkingDataUtils.initModel(orderRecordModel));
            if(orderRecordModel.getStatus() == E_RechargeStatus.PAY_SUCCESS.getCode())
                TalkingDataUtils.bq.add(TalkingDataUtils.initTdModel(orderRecordModel));
        } catch (Exception e) {
            LogUtils.errorException(e);
            LogUtils.error("冲值初始化错误，param -> " + JSONObject.toJSONString(orderRecordModel));
            return 0;
        }
        return result;
    }

    /**
     *
     * @param newOrder
     * @param checkHistory 是否要检测数据库的数据
     */
    @Override
    public void updateOrder(OrderRecordModel newOrder, boolean checkHistory)
    {
        Date now = new Date();
        if(newOrder.getStatus() == E_RechargeStatus.PAY_SUCCESS.getCode())
            newOrder.setCompleteTime(now);
        if(checkHistory)
        {
            OrderRecordModel historyOrder = orderRecordMapper.findByOutTradeNo(newOrder.getOutTradeNo());
            if (historyOrder == null){
                //为空要insert
                newOrder.setCreateTime(now);
                orderRecordMapper.insert(newOrder);
                //订单完成
            }else{
                newOrder.setId(historyOrder.getId());
                orderRecordMapper.update(newOrder);
            }
        }
        else
        {
            orderRecordMapper.update(newOrder);
        }

        try {
            if (newOrder.getPartner() == 0){
                RoleRecordModel recordModel = roleRecordMapper.getByObjectIndex(newOrder.getObjectIndex());
                if (recordModel == null){
                    throw new Exception("玩家不存在请核对流水号 objectIndex->" + newOrder.getObjectIndex());
                }
                if (recordModel.getServerRuleId() == null || recordModel.getServerRuleId() == 0){
                    throw new Exception("玩家为老玩家未保存serverRuleId 无法进行离线talkingData数据发送");
                }
                newOrder.setPartner(recordModel.getServerRuleId());
            }
            if(newOrder.getStatus() == E_RechargeStatus.PAY_SUCCESS.getCode())
            {
                TalkingDataUtils.bq.add(TalkingDataUtils.initModel(newOrder));
                TalkingDataUtils.bq.add(TalkingDataUtils.initTdModel(newOrder));
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public int findCount(String objectIndex, String tcd) {
        return orderRecordMapper.findCount(objectIndex,tcd);
    }

    @Override
    public Integer getTotalRechargeCount(String objectIndex, String startTime, String endTime) {
        return orderRecordMapper.getTotalRechargeCount(objectIndex,startTime,endTime);
    }

    @Override
    public OrderRecordModel findByOutTradeNo(String outTradeNo) {
        return orderRecordMapper.findByOutTradeNo(outTradeNo);
    }

    @Override
    public OrderRecordModel findByTcd(String tcd) {
        return orderRecordMapper.findByTcd(tcd);
    }

    @Override
    public OrderRecordModel findNoPayOrder(String playerIndex, int serverId) {
        return orderRecordMapper.findStatusOrder(playerIndex, serverId, E_RechargeStatus.NO_PAY.getCode());
    }

}
